<template>
	<div class="app-container">
		<el-form
			:model="queryParams"
			ref="queryForm"
			:inline="true"
			v-show="showSearch"
			label-width="68px"
		>
			<el-form-item label="开始时间" prop="startTime">
				<div class="block">
					<el-date-picker
						v-model="queryParams.startTime"
						value-format="yyyy-MM-dd"
						type="date"
						placeholder="选择日期"
					></el-date-picker>
				</div>
			</el-form-item>
			<el-form-item label="结束时间" prop="endTime">
				<div class="block">
					<el-date-picker
						v-model="queryParams.endTime"
						type="date"
						value-format="yyyy-MM-dd"
						placeholder="选择日期"
					></el-date-picker>
				</div>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam:address:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['exam:address:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam:address:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['exam:address:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>-->
		<div style=" border: 1px solid #E4E7ED; width='100%'">
			<el-table
				v-loading="loading"
				border
				:data="addressList"
				@selection-change="handleSelectionChange"
			>
				<!--      <el-table-column label="其他描述" align="center" prop="id" />-->
				<el-table-column label="名称" align="center" :show-overflow-tooltip="true" prop="name" />
				<el-table-column label="场次" align="center" :show-overflow-tooltip="true" prop="sessionName" />
				<!-- <el-table-column label="考场" align="center" :show-overflow-tooltip="true" prop="capacity" /> -->
				<el-table-column
					label="开始时间"
					align="center"
					:show-overflow-tooltip="true"
					prop="sessionStartTime"
				/>
				<el-table-column
					label="结束时间"
					align="center"
					:show-overflow-tooltip="true"
					prop="sessionEndTime"
				/>
				<el-table-column
					label="试卷"
					align="center"
					:show-overflow-tooltip="true"
					prop="paperName"
					width="600"
				/>
				<el-table-column label="考生状态" align="center" :show-overflow-tooltip="true" prop="state">
					<template slot-scope="scope">
						<div v-if="scope.row.state == 0">其他</div>
						<div v-if="scope.row.state == 1">未开始考试</div>
						<div v-if="scope.row.state == 2">正在考试</div>
						<div v-if="scope.row.state == 3">已交卷</div>
						<div v-if="scope.row.state == 4">批阅已完成</div>
						<div v-if="scope.row.state == 5">考试状态异常</div>
					</template>
				</el-table-column>
				<el-table-column
					label="操作"
					align="center"
					width="400"
					:show-overflow-tooltip="true"
					class-name="small-padding fixed-width"
				>
					<template slot-scope="scope">
						<el-button
							size="mini"
							type="text"
							icon="el-icon-edit"
							:disabled="scope.row.attend"
							@click="handleUpdate(scope.row)"
						>进入考试</el-button>
						<el-button size="mini" type="text" icon="el-icon-time" @click="handTimelate(scope.row)">延时考试</el-button>
						<!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >进入考试</el-button
						>-->
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination
			v-show="total > 0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>
		<!-- 添加或修改考场管理对话框 -->
		<el-dialog
			fullscreen
			:title="title"
			:visible.sync="open"
			width="100%"
			append-to-body
			class="exam-dialog"
		>
			<exam v-if="open" :row="row" @ok="ok"></exam>
		</el-dialog>
		<!-- 延长考试时间 -->
		<el-dialog :title="title" :visible.sync="timeLate" width="30%" append-to-body>
			<el-form ref="timeLateform" :model="timeLateform" :rules="timeLateformrules" label-width="100px">
				<el-form-item label="考生id" prop="examineeId" v-show="false">
					<el-input v-model="timeLateform.examineeId" placeholder="请输入考生id" />
				</el-form-item>
				<el-form-item label="场地id" prop="sessionId" v-show="false">
					<el-input v-model="timeLateform.sessionId" placeholder="请输入考生id" />
				</el-form-item>
				<el-form-item label="延长时间" prop="time">
					<el-input type="number" v-model="timeLateform.time" placeholder="请输入延长时间（分钟）" />
				</el-form-item>
				<el-form-item label="延时口令" prop="pwd">
					<el-input type="password" v-model="timeLateform.pwd" placeholder="请输入延时口令" />
				</el-form-item>
			</el-form>
			<div slot="footer" class>
				<el-button type="primary" @click="timelLatesubmitForm">确 定</el-button>
				<el-button @click="timeLate = false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listExamiton, postTimelate } from '@/api/examination/examination'
import exam from './exam.vue'

export default {
	name: 'Address',
	components: { exam },
	data() {
		return {
			//延长考试时间
			timeLate: false,
			// 遮罩层
			loading: true,
			// 导出遮罩层
			exportLoading: false,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 考场管理表格数据
			addressList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				startTime: '',
				endTime: '',
			},
			//延长考试
			timeLateform: {
				examineeId: '',
				sessionId: '',
				time: 0,
				pwd: '',
			},
			//组件通信
			row: [],
			// 表单参数
			form: {},
			// 表单校验
			// 表单校验
			rules: {
				name: [{ required: true, message: '场地名称不能为空', trigger: 'blur' }],
			},
			timeLateformrules: {
				time: [{ required: true, message: '延时时间不能为0', trigger: 'blur' }],
				pwd: [{ required: true, message: '请输入延时口令', trigger: 'blur' }],
			},
		}
	},
	created() {
		this.getList()
		//?????????????????
		// let fullscreenEnabled = document.fullscreenEnabled||document.mozFullScreenElement ||document.webkitFullscreenElement;;
		// console.log(fullscreenEnabled, document.fullscreenEnabled, "是否兼容全屏");
	},
	methods: {
		//子组件通信，触发父子间的事件
		ok(message) {
			this.open = false
			this.getList()
		},
		/** 查询考场管理列表 */
		getList() {
			this.loading = true
			listExamiton(this.queryParams).then((response) => {
				console.log(this.queryParams)
				this.addressList = response.rows
				if (this.addressList != null) {
					for (let i = 0; i < this.addressList.length; i++) {
						this.countDown(this.addressList[i])
					}
				}
				this.total = response.total
				this.loading = false
			})
		},
		//延长考试时间
		handTimelate(templatedata) {
			this.timeLateform = {
				examineeId: templatedata.examineeId,
				sessionId: templatedata.sessionId,
				time: 0,
				pwd: '',
			}
			this.timeLate = true
		},
		//提交延长考试时间
		timelLatesubmitForm() {
			this.$refs['timeLateform'].validate((valid) => {
				if (valid) {
					this.timeLateform.time = parseFloat(this.timeLateform.time)
					postTimelate(this.timeLateform).then((response) => {
						console.log('延迟时间', response)
						this.getList()
						this.timeLate = false
					})
				}
			})
		},
		//处理时间
		countDown(templatedata) {
			// let now = new Date(); // 获取当前时间
			let now = templatedata.currTime
			if (
				Date.parse(templatedata.sessionStartTime) < Date.parse(now) &&
				Date.parse(now) < Date.parse(templatedata.sessionEndTime) &&
				(templatedata.state == 1 || templatedata.state == 2)
			) {
				templatedata.attend = false
			} else {
				templatedata.attend = true
			}
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				name: null,
				code: null,
				capacity: null,
				address: null,
				state: null,
				addressExplain: null,
			}
			this.resetForm('form')
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm')
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map((item) => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = '添加考场管理'
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			// this.reset();
			// const id = row.id || this.ids
			// getAddress(id).then(response => {
			//   this.form = response.data;
			//   this.open = true;
			//   this.title = "修改考场管理";
			// });
			this.row = row
			this.open = true
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					if (this.form.id != null) {
						updateAddress(this.form).then((response) => {
							this.msgSuccess('修改成功')
							this.open = false
							this.getList()
						})
					} else {
						addAddress(this.form).then((response) => {
							this.msgSuccess('新增成功')
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$confirm('是否确认删除考场管理编号为"' + ids + '"的数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delAddress(ids)
				})
				.then(() => {
					this.getList()
					this.msgSuccess('删除成功')
				})
				.catch(() => {})
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams
			this.$confirm('是否确认导出所有考场管理数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					this.exportLoading = true
					return exportAddress(queryParams)
				})
				.then((response) => {
					this.download(response.msg)
					this.exportLoading = false
				})
				.catch(() => {})
		},
	},
}
</script>
<style scoped lang="scss">
.exam-dialog::v-deep {
	.el-dialog__header {
		display: none;
		// position: fixed;
		// left: 0;
		// top: 0;
		// width: 100%;
		// height: 50px;
		// line-height: 50px;
		// padding: 0;
	}
	.el-dialog__body {
		padding: 0;
		height: 100%;
		overflow: hidden;
	}
}
</style>
